import { _decorator, AnimationClip, Component, Event, Node, Animation, SpriteFrame } from 'cc'
import { FSM_PARAMS_TYPE_ENUM, PARAMS_NAME_ENUM } from '../Enums'
import State from '../Base/State'
import { getInitParamsNumber, getInitParamsTriggle, StateMachine } from '../Base/StateMachine'
import IdleSubStateMachine from './IdleSubStateMachine'
import DeathSubStateMachine from './DeathSubStateMachine'
const { ccclass, property } = _decorator

@ccclass('DoorStateMachine')
export class DoorStateMachine extends StateMachine {
  async init() {
    // 因为只需要一个播放动画的 animationComponent，所以放在状态机这里，而不是放在state里
    this.animationComponent = this.addComponent(Animation)

    // 初始化参数列表
    this.initParams()
    // 初始化各类动画状态
    this.initStateMachines()

    await Promise.all(this.waitingList)
  }

  initParams() {
    // 增加了 idle动画参数
    this.params.set(PARAMS_NAME_ENUM.IDLE, getInitParamsTriggle())
    this.params.set(PARAMS_NAME_ENUM.DEATH, getInitParamsTriggle())
    // 增加方向参数
    this.params.set(PARAMS_NAME_ENUM.DIRECTION, getInitParamsNumber())
  }
  // 初始化动画播放事件监听
  initAnimationEvent() {}

  initStateMachines() {
    // 初始化 turnleft动画
    this.stateMachines.set(PARAMS_NAME_ENUM.IDLE, new IdleSubStateMachine(this))
    this.stateMachines.set(PARAMS_NAME_ENUM.DEATH, new DeathSubStateMachine(this))
  }

  run() {
    // 判断当前状态机类型
    switch (this.currentState) {
      case this.stateMachines.get(PARAMS_NAME_ENUM.IDLE):
      case this.stateMachines.get(PARAMS_NAME_ENUM.DEATH):
        // 判断当前动画参数，哪个动画被触发，将被触发的动画状态设置为当前动画状态
        if (this.params.get(PARAMS_NAME_ENUM.IDLE).value) {
          this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.IDLE)
        } else if (this.params.get(PARAMS_NAME_ENUM.DEATH).value) {
          this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.DEATH)
        } else {
          this.currentState = this.currentState
        }
        break
      default:
        this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.IDLE)
        break
    }
  }
}
